package com.example.authdemo.expression;

import com.example.authdemo.domain.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * ClassName: CustomExpressionRoot
 * Package: com.example.authdemo.expression
 * Description:
 *
 * @Author luohf
 * @Create 2023/7/27 0:50:40
 */
@Component("ex")
public class CustomExpressionRoot {

    public boolean hasAuthority(String authority) {
        //获取当前用户的权限
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        List<String> permissions = loginUser.getPermissions();
        System.out.println("走到了自己的CustomExpressionRoot中");
        //判断用户权限集合中是否存在authority
        return permissions.contains(authority);
    }
}
